package net.lanelife.casino.cms.web.controller;

import java.util.List;

import javax.servlet.ServletRequest;
import javax.validation.Valid;

import net.lanelife.casino.caipiao.dto.admin.BankTransactionDetailCreateDTO;
import net.lanelife.casino.caipiao.dto.admin.BankTransactionDetailDTO;
import net.lanelife.casino.caipiao.dto.admin.BankTransactionDetailTable;
import net.lanelife.casino.caipiao.dto.admin.BankTransactionDetailUpdateDTO;
import net.lanelife.casino.caipiao.entity.BankTransactionDetail;
import net.lanelife.casino.caipiao.entity.BankTransactionDetail.SourceUsesType;
import net.lanelife.casino.caipiao.service.api.IBankTransactionDetailService;
import net.lanelife.casino.common.repository.DynamicSpecifications;
import net.lanelife.casino.common.repository.SearchFilter;
import net.lanelife.casino.common.web.controller.GenericController;
import net.lanelife.casino.common.web.resolver.ViewResolver;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping(value = BankTransactionDetailController.PATH + "-{uses}")
public class BankTransactionDetailController extends GenericController {
	
	public static final String PATH = "/bank-transaction-detail";

	@Autowired
	private IBankTransactionDetailService bankTransactionDetailService;
	
	
	@RequestMapping(method = RequestMethod.GET)
	public String list(Model model, @PathVariable String uses) throws Exception {
		return ViewResolver.getView(PATH + "/list");
	}

	@SuppressWarnings("unchecked")
	@RequestMapping(method = RequestMethod.POST)
	public String list(Model model, @PathVariable String uses, Pageable pageable, ServletRequest request) throws Exception {
		List<SearchFilter> filters = SearchFilter.parse(request);
		filters.add(new SearchFilter("sourceUsesType", SearchFilter.Operator.EQ, uses.toUpperCase()));
		model.addAllAttributes(bankTransactionDetailService.findTable(BankTransactionDetailTable.class, BankTransactionDetailDTO.class, DynamicSpecifications.<BankTransactionDetail>bySearchFilter(filters), pageable).map());
		return ViewResolver.getView(PATH + "/list");
	}
	
	@RequestMapping(value = { "/create" }, method = RequestMethod.GET)
	public String create(Model model, @PathVariable String uses) throws Exception {
		BankTransactionDetailCreateDTO createDTO = new BankTransactionDetailCreateDTO();
		model.addAttribute("createDTO", createDTO);
		return ViewResolver.getView(PATH + "/create");
	}
	
	@RequestMapping(value = { "/create" }, method = RequestMethod.POST)
	public String create(Model model, @PathVariable String uses, @Valid @ModelAttribute BankTransactionDetailCreateDTO createDTO, BindingResult br) throws Exception {
		if (!br.hasErrors()) {
			createDTO.setSourceUsesType(SourceUsesType.valueOf(uses.toUpperCase())); // 设置用途
			bankTransactionDetailService.save(createDTO);
		}
		return ViewResolver.getView(PATH + "/create");
	}
	
	@RequestMapping(value = { "/update/{id}" }, method = RequestMethod.GET)
	public String update(Model model, @PathVariable String uses, @PathVariable Integer id) throws Exception {
		BankTransactionDetailUpdateDTO updateDTO = bankTransactionDetailService.find(BankTransactionDetailUpdateDTO.class, id);
		model.addAttribute("updateDTO", updateDTO);
		return ViewResolver.getView(PATH + "/update");
	}
	
	@RequestMapping(value = { "/update/{id}" }, method = RequestMethod.POST)
	public String update(@PathVariable String uses, @Valid @ModelAttribute BankTransactionDetailUpdateDTO updateDTO, BindingResult br) throws Exception {
		if (!br.hasErrors()) {
			updateDTO.setSourceUsesType(SourceUsesType.valueOf(uses.toUpperCase())); // 设置用途
			bankTransactionDetailService.save(updateDTO);
		}
		return ViewResolver.getView(PATH + "/update");
	}
	
	@RequestMapping(value = { "/delete/{id}" }, method = RequestMethod.GET)
	public String delete(@PathVariable Integer id) throws Exception {
		bankTransactionDetailService.delete(id);
		return ViewResolver.getView(PATH + "/list");
	}
}
